package com.gpnu7_412.yanzhi.controller.dynamic;

import com.github.pagehelper.PageInfo;
import com.gpnu7_412.yanzhi.dto.DynamicDisplayDto;
import com.gpnu7_412.yanzhi.entity.Dynamic;
import com.gpnu7_412.yanzhi.entity.DynamicHanding;
import com.gpnu7_412.yanzhi.exception.custom.RequestDataException;
import com.gpnu7_412.yanzhi.service.DynamicService;
import com.gpnu7_412.yanzhi.status.Impl.ResultCode;
import com.gpnu7_412.yanzhi.util.Result;
import com.gpnu7_412.yanzhi.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZZH
 */
@CrossOrigin
@RestController
@RequestMapping("/dynamic")
public class DynamicController {
    @Autowired
    private DynamicService dynamicService;

    /**
     * 发布动态
     * @param dynamic 新增动态对象
     * @return 响应对象
     */
    @PostMapping("/add")
    public Result addDynamic(@RequestBody Dynamic dynamic) {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        dynamic.setPublishTime(timestamp);
        Map<String, Object> map = ThreadLocalUtil.get();
        dynamic.setUserId((Integer) map.get("userId"));
        if (!dynamicService.releaseDynamic(dynamic)) {
            throw new RequestDataException("参数为空");
        }
        return new Result(ResultCode.SUCCESS, "动态发布成功");
    }

    /**
     * 删除动态
     * @param dynamicId 待删动态ID
     * @return 响应对象
     */
    @DeleteMapping("/delete")
    public Result deleteDynamic(@RequestParam("dynamicId") Integer dynamicId) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (!dynamicService.deleteDynamic(dynamicId, userId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result(ResultCode.SUCCESS, "动态删除成功");
    }

    /**
     * 修改动态信息
     * @param dynamic 修改后的动态对象
     * @return 响应对象
     */
    @PutMapping("/update")
    public Result updateDynamic(@RequestBody Dynamic dynamic) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        if (!dynamicService.updateDynamic(dynamic, userId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result(ResultCode.SUCCESS, "动态修改成功");
    }

    /**
     * 动态点赞
     * @param requestMap 请求信息，需包含：dynamicId 点赞动态ID
     * @return 响应对象
     */
    @PutMapping("/like")
    public Result likeDynamic(@RequestBody Map<String, Object> requestMap) {
        Integer dynamicId = (Integer) requestMap.get("dynamicId");
        if (!dynamicService.likeDynamic(dynamicId)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 动态举报
     * @param requestMap 请求信息，需包含：dynamicId 举报动态ID，content 举报理由
     * @return 响应对象
     */
    @PostMapping("/report")
    public Result reportDynamic(@RequestBody Map<String, Object> requestMap) {
        DynamicHanding dynamicHanding = new DynamicHanding();
        dynamicHanding.setDynamicId((Integer) requestMap.get("dynamicId"));
        dynamicHanding.setContent((String) requestMap.get("content"));
        dynamicHanding.setHandingTime(new Timestamp(System.currentTimeMillis()));
        if (!dynamicService.reportDynamic(dynamicHanding)) {
            throw new RequestDataException("参数为空");
        }
        return new Result();
    }

    /**
     * 根据动态ID获取动态展示对象
     * @param dynamicId 动态ID
     * @return 响应对象
     */
    @GetMapping("/select/id")
    public Result selectById(@RequestParam("dynamicId") Integer dynamicId) {
        DynamicDisplayDto dynamicDisplayDto = dynamicService.getDynamicById(dynamicId);
        if (dynamicDisplayDto == null) {
            throw new RequestDataException("参数为空");
        }
        return new Result(dynamicDisplayDto);
    }

    /**
     * 根据用户ID查找其发布的所有动态
     * @param userId 用户ID
     * @return 响应对象
     */
    @GetMapping("/select/userId")
    public Result selectByUserId(@RequestParam("userId") Integer userId) {
        List<Dynamic> dynamicList = dynamicService.getDynamicByUserId(userId);
        if (dynamicList == null) {
            throw new RequestDataException("参数为空");
        }
        Map<String, Object> responseMap = new HashMap<>(2);
        responseMap.put("totalCount", dynamicList.size());
        responseMap.put("list", dynamicList);
        return new Result(responseMap);
    }

    /**
     * 查看全部动态展示
     * @return 响应对象
     */
    @GetMapping("/select/all")
    public Result selectAll(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageInfo<DynamicDisplayDto> dynamicDisplayDtoPageInfo = dynamicService.getAllDynamic(pageNum, pageSize);
        return new Result(dynamicDisplayDtoPageInfo);
    }

    /**
     * 查看全部关注者动态
     * @return 响应数据
     */
    @GetMapping("/select/attention")
    public Result selectByAttention(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        PageInfo<DynamicDisplayDto> dynamicDisplayDtoPageInfo = dynamicService.getAllAttention(pageNum, pageSize, userId);
        return new Result(dynamicDisplayDtoPageInfo);
    }

    /**
     * 根据标题模糊查找动态
     * @param title 标题
     * @return 响应数据
     */
    @GetMapping("/select/title")
    public Result selectByTitle(@RequestParam("title") String title) {
        List<DynamicDisplayDto> dynamicList = dynamicService.getDynamicByTitle(title);
        Map<String, Object> result = new HashMap<>(2);
        result.put("totalCount", dynamicList.size());
        result.put("list", dynamicList);
        return new Result(result);
    }
}
